package com.laint.hear.app;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.laint.hear.utils.ConstantsValue;

/**
 * Created by leiyingtao on 2016/10/12.
 * APP状态管理
 */

public class AppStatusTracker implements Application.ActivityLifecycleCallbacks {

    private int mAppStatus = ConstantsValue.KILLED;
    private boolean mIsforeground;
    private int mActiveCount;
    private static AppStatusTracker mTracker;

    private AppStatusTracker(Application application){
        application.registerActivityLifecycleCallbacks(this);
    }

    public static void init(Application application){
        mTracker = new AppStatusTracker(application);
    }

    public static AppStatusTracker getInstance(){
        return mTracker;
    }

    public boolean getIsforeground(){
        return mIsforeground;
    }

    public void setmAppStatus(int status){
        mAppStatus = status;
    }

    public  int getmAppStatus(){
        return mAppStatus;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {
        mActiveCount++;
    }

    @Override
    public void onActivityResumed(Activity activity) {
        mIsforeground =true;
    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        mActiveCount--;
        if(mActiveCount == 0){
            mIsforeground = false;
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}
